<?php
namespace my;
/**
 * 前端静态类
 * 前端交互，错误捕捉
 * @author xls
 */
class Front{
    public static $error  = [];
    public static $config = [        
        //业务参数
        'r'     => 0,
        'code'  => 200,
        'msg'   => '',
        'name'  => '',
        'data'  => '',
        
        //前端处理动作
        'err'  => [
            'msg'           => '', #重置提示文字
            'url'           => '', #错误之后跳转url
            'return_type'   => 'json',  # json|return
            'js_action'     => 'error', # alert|error|close|reload 前端js处理标识
        ],
        'suc'  => [
            'msg'           => '',
            'url'           => '', 
            'return_type'   => 'json',
            'js_action'     => '', 
        ],   
    ];
    
    /**
     * 验证错误，并处理前端交互
     * @param unknown $config  返回前端参数
     * @param string $is_api   是否是api
     * @return 返回 bool or exit json
     */
    public static function check( $config = [], $is_api = false ){
        //合并
        if( isset($config['err']) )
            $config['err'] = array_merge(self::$config['err'], $config['err']);
        if( isset($config['suc']) )
            $config['suc'] = array_merge(self::$config['suc'], $config['suc']);        
        $config = array_merge(self::$config, $config);
        
        //错误赋值给前端
        if( isset(self::$error[0]) ){
            $config['r']      =  1;
            while ( list ($key, $val) = each (self::$error[0]) ){
                if((string)($key) != 'err_code'){
                    $config['name']   = $key;
                    $config['msg']    = $val;
                }else{
                    $config['code']   = $val;
                }
            }
            $type = $config['err']['return_type'];
            if(!empty($config['err']['msg']))
                $config['msg'] = $config['err']['msg'];            
        }else{
            $type = $config['suc']['return_type'];
            if(!empty($config['suc']['msg']))
                $config['msg'] = $config['suc']['msg'];
        }
        
        if($is_api){
            unset($config['err']);
            unset($config['suc']);
            exit(json_encode($config, JSON_UNESCAPED_UNICODE));
        }
        
        //输入行为
        switch ( $type ){
            case 'return':
                return !isset(self::$error[0]);
            case 'json':
            default:
                exit(json_encode($config, JSON_UNESCAPED_UNICODE));
        }   
    }
    
    /**
     * 错误捕捉
     * @param unknown $error_data 一维数组 $k对应前端name, $v对应错误提示
     * @param $code 错误码 
     * @return false;
     */
    public static function error( $error_data = [], $code = 100 ){
        foreach($error_data as $k => $v){
            self::$error[] = [$k=>$v, 'err_code'=>$code];
        }
        return false;
    }
    
    /**
     * 获取错误信息
     */
    public static function get_error_msg(){
        return self::$error[0][0];
    }
    
    /**
     * URL重定向
     * @param string  $url   重定向的URL地址 
     * @param integer $time  重定向的等待时间（秒）
     * @param string  $msg   重定向前的提示信息
     * @return 跳转页面
     */
    public static function redirect($url, $time = 0, $msg = '') {
        if (empty($msg)) 
            $msg = "系统将在{$time}秒之后自动跳转到{$url}！";
    
        if (!headers_sent()) {
            if (0 === $time) {
                header('Location: ' . $url);
            } else {
                header("refresh:{$time};url={$url}");
                echo ($msg);
            }
        } else {
            $str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
            if ($time != 0) {
                $str .= $msg;
            }
            echo ($str);
        }
        exit;
    }
}